﻿package mortal.game.scene3D.fight
{
    import Message.DB.Tables.*;
    import baseEngine.modifiers.*;
    import com.gengine.utils.*;
    import flash.events.*;
    import frEngine.event.*;
    import mortal.game.resource.tableConfig.*;
    import mortal.game.scene3D.events.*;
    import mortal.game.scene3D.model.data.*;
    import mortal.game.scene3D.model.player.*;
    import mortal.game.scene3D.player.entity.*;

    public class FightEffectLead extends FightEffectBase
    {
        protected var skillModel:TSkillModel;
        protected var effectPlayer:EffectPlayer;

        public function FightEffectLead()
        {
            return;
        }// end function

        override public function runStart() : void
        {
            maxTime = 12000;
            super.runStart();
            if (!_fromPlayer || !_fromPlayer.parent)
            {
                this.runFinish();
                return;
            }
            if (_fromPlayer is MonsterPlayer && (_fromPlayer as MonsterPlayer).bossInfo.aiBehavior & 8)
            {
            }
            else if (_targetPoint)
            {
                _fromPlayer.direction = MathUitl.getAngleByXY(_fromPlayer.x2d, _fromPlayer.y2d, _targetPoint.x, _targetPoint.y);
            }
            else if (_targetPlayer && _targetPlayer != _fromPlayer)
            {
                _fromPlayer.direction = MathUitl.getAngleByXY(_fromPlayer.x2d, _fromPlayer.y2d, _targetPlayer.x2d, _targetPlayer.y2d);
            }
            this.skillModel = SkillModelConfig.instance.getInfoById(_skill.skillModel);
            if (!this.skillModel)
            {
                this.runFinish();
                return;
            }
            if (this.skillModel.action && ActionType.isLeadStartAction(this.skillModel.action))
            {
                FrEventDispatcher.instance.proxyAddEventListener(_fromPlayer, PlayerEvent.PLAYER_LEADING, this.leadingHandler);
                FrEventDispatcher.instance.proxyAddEventListener(_fromPlayer, PlayerEvent.PLAYER_LEADING_END, this.leadingEndHandler);
                _fromPlayer.setAction(ActionType.leadStart, this.skillModel.action);
                if (this.skillModel && _fromPlayer && _fromPlayer.actionName != this.skillModel.action)
                {
                    if (this.skillModel.chainEffect && _fromPlayer && _targetPlayer)
                    {
                        FightChainUtil.addChain(_fromPlayer, _targetPlayer, this.skillModel.chainEffect, _skill.leadTime);
                    }
                    FrEventDispatcher.instance.proxyRemoveEventListener(_fromPlayer, PlayerEvent.PLAYER_LEADING, this.leadingHandler);
                    FrEventDispatcher.instance.proxyRemoveEventListener(_fromPlayer, PlayerEvent.PLAYER_LEADING_END, this.leadingEndHandler);
                    this.runFinish();
                }
            }
            else if (this.skillModel.leadAction == ActionName.Tornado)
            {
                _fromPlayer.setAction(ActionType.leading, this.skillModel.leadAction);
                FrEventDispatcher.instance.proxyAddEventListener(_fromPlayer, PlayerEvent.PLAYER_LEADING_END, this.leadingEndHandler);
                this.leadingHandler(null);
            }
            else
            {
                if (this.skillModel.action)
                {
                    _fromPlayer.setAction(ActionType.attack, this.skillModel.action);
                }
                this.leadingHandler(null);
                FrEventDispatcher.instance.proxyAddEventListener(_fromPlayer, PlayerEvent.PLAYER_LEADING_END, this.leadingEndHandler);
            }
            return;
        }// end function

        protected function leadingHandler(event:Event) : void
        {
            if (FightEffectUtil.isShieldSkill())
            {
                return;
            }
            if (this.skillModel.selfModel && _isNotShildEffect)
            {
                this.effectPlayer = SkillEffectUtil.addPlayerEffect(_fromPlayer, this.skillModel.selfModel, true, _isFromRolePlayer, null, PlayMode.ANIMATION_LOOP_MODE);
            }
            if (this.skillModel.chainEffect && _fromPlayer && _targetPlayer && _isNotShildEffect)
            {
                FightChainUtil.addChain(_fromPlayer, _targetPlayer, this.skillModel.chainEffect, _skill.leadTime);
            }
            return;
        }// end function

        protected function leadingEndHandler(event:Event) : void
        {
            this.runFinish();
            return;
        }// end function

        protected function removeListeners() : void
        {
            FrEventDispatcher.instance.proxyRemoveEventListener(_fromPlayer, PlayerEvent.PLAYER_LEADING, this.leadingHandler);
            FrEventDispatcher.instance.proxyRemoveEventListener(_fromPlayer, PlayerEvent.PLAYER_LEADING_END, this.leadingEndHandler);
            return;
        }// end function

        override public function runFinish() : void
        {
            if (this.effectPlayer)
            {
                this.effectPlayer.parent = null;
                this.effectPlayer.dispose();
                this.effectPlayer = null;
            }
            if (this.skillModel && this.skillModel.chainEffect && _fromPlayer && _targetPlayer)
            {
                FightChainUtil.removeChainByEntity(_fromPlayer, _targetPlayer, this.skillModel.chainEffect);
            }
            this.removeListeners();
            super.runFinish();
            this.skillModel = null;
            return;
        }// end function

    }
}
